var setting = {
	data : {simpleData: {enable: true}},
    showLine : true,
    async: {
        enable: true,  
        dataFilter: filter  
    },
	callback: {
		//beforeClick: beforeClick,
		onClick: onClick
	}
    
};
function filter(treeId, parentNode, childNodes) {  
    if (!childNodes) return null;  
    for (var i=0, l=childNodes.length; i<l; i++) {  
    	//childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
        childNodes[i].name = childNodes[i].name.replace('','');  
    }  
    return childNodes;  
} 
function onClick(event, treeId, treeNode, clickFlag) {
	console.log("id"+treeNode.id);
	var treeObj = $.fn.zTree.getZTreeObj(treeId);
	treeObj.expandNode(treeNode, true, true, true);
	if(treeNode.level==2&&treeNode.children==null){
		$.getJSON("ztreeaddNodes",{"id":treeNode.id},function(nodes,status){
			var newNodes = nodes;
			console.log(newNodes.length);
			var treeObj = $.fn.zTree.getZTreeObj(treeId);  
			newNodes = treeObj.addNodes(treeNode, newNodes, true);
			
			//var childnodes = treeNode.children;
			//console.log(childnodes);
			treeObj.expandNode(treeNode, true, true, true);
			
		});
	}
	else if(treeNode.level==3){
		$.post("showmetadata",{"id":treeNode.id},function(data,status){
			data=$.parseJSON(data);
			libsid=data['libsid'];
			data=data['metadata'];
			$("#ml").hide();
			$("#mljg").show();
			var table = $('#shows');
			var count=1;
			var ss=null;
			 var sdate = new Date(data.buildDate); 
	         var datestr =bu0(sdate.getMonth()+1)+'/'+bu0(sdate.getDate())+'/'+sdate.getFullYear();
	         var sdate2 = new Date(libsid.buildDate); 
	         var datestr2 =bu0(sdate2.getMonth()+1)+'/'+bu0(sdate2.getDate())+'/'+sdate2.getFullYear();
	         var sdate3 = new Date(libsid.startTime); 
	         var datestr3 =bu0(sdate3.getMonth()+1)+'/'+bu0(sdate3.getDate())+'/'+sdate3.getFullYear();
	         
			console.log("lv3 "+data);
			table.html('');
			table.append('<tr  id="focusme" align="center"><td colspan=4>'+'元数据信息'+'</td></tr>');
			table.append('<tr><td colspan=2>元数据编码 </td><td colspan=2>'+data['mdCode']+'</td></tr>');
			table.append('<tr><td>元数据名称</td><td>'+data['mdName']+'</td><td>地区编码 </td><td>'+data['areaCod']+'</td></tr>');
			table.append('<tr><td>元数据创建单位</td><td>'+data['buildUnit']+'</td><td>创建人</td><td>'+data['staff']+'</td></tr>');
			table.append('<tr><td>创建日期</td><td>'+datestr+'</td><td>元数据标准 </td><td>'+data['standName']+'</td></tr>');
			table.append('<tr><td>一级分类编码</td><td>'+data['firstlev']+'</td><td>二级分类编码</td><td>'+data['secondlev']+'</td></tr>');
			table.append('<tr><td>三级分类编码</td><td>'+data['thirdlev']+'</td><td>绑定数据库owner</td><td>'+data['owner']+'</td></tr>');
			table.append('<tr><td colspan=2>绑定数据库URL</td><td colspan=2>'+data['location']+'</td></tr>');
			table.append('<tr  align="center"><td colspan=4>'+'绑定数据库信息'+'</td></tr>');
			table.append('<tr><td>数据库创建单位</td><td>'+libsid['buildUnit']+'</td><td>创建人</td><td>'+libsid['staff']+'</td></tr>');
			table.append('<tr><td>数据库管理单位</td><td>'+libsid['buildUnit']+'</td><td>管理人</td><td>'+libsid['staff']+'</td></tr>');
			table.append('<tr><td>数据库创建时间</td><td>'+datestr2+'</td><td>数据集覆盖起始时间</td><td>'+datestr3+'</td></tr>');
			table.append('<tr><td>关键词</td><td>'+libsid['keywords']+'</td><td>备注</td><td>'+libsid['beizhu']+'</td></tr>');
			table.append('<tr><td>建库标准</td><td>'+libsid['standname']+'</td><td>安全级别</td><td>'+libsid['seclevl']+'</td></tr>');
			table.append('<tr><td>使用限制</td><td>'+libsid['useconstr']+'</td><td>其他限制</td><td>'+libsid['otherconstr']+'</td></tr>');
			table.append('<tr><td>注意事项</td><td>'+libsid['attention']+'</td><td>数据交换环境</td><td>'+libsid['exchangeEnv']+'</td></tr>');
			table.append('<tr><td>字符集</td><td>'+libsid['charSet']+'</td><td>数据库服务器访问说明</td><td>'+libsid['service']+'</td></tr>');
			table.append('<tr><td colspan=2>数据库版本</td><td colspan=2>'+libsid['version']+'</td></tr>');
//			for(var x in data){
//				var d =data[x];
//				if(count==1){ ss='<tr><td>'+x+'</td><td>'+d+'</td>',count++}
//				else if(count==2){
//					ss +='<td>'+x+'</td><td>'+d+'</td></tr>';
//					table.append(ss);
//					count=1;
//				}
//				
//				console.log(d);
//			}
//			  $("#focusme")[0].focus();
//			  $('#shows')[0].focus();
			 
		
		});
	}else if(treeNode.level==4){
		$.get("showtablesAndcolunms",{"id":treeNode.id},function(data,status){
			data=$.parseJSON(data);
			$("#ml").hide();
			$("#mljg").show();
			var objsid=data['objdsid'];
			var classsid=data['classdsid'];
			var table = $('#shows');
			 var sdate = new Date(classsid.buildDate); 
	         var datestr =bu0(sdate.getMonth()+1)+'/'+bu0(sdate.getDate())+'/'+sdate.getFullYear();
		//	console.log(objsid);
			table.html('');
			table.append('<tr  align="center"><td colspan=4>'+'表信息'+'</td></tr>');
			table.append('<tr><td>表英文名</td><td>'+classsid.dsName+'</td><td>'+'表中文名'+'</td><td>'+classsid.beizhu+'</td></tr>');
			table.append('<tr><td>创建日期</td><td>'+datestr+'</td><td>'+'关键字'+'</td><td>'+classsid.keywords+'</td></tr>');
			table.append('<tr><td colspan=2>三级分类编码</td><td colspan=2>'+classsid.levelcode+'</td></tr>');
			table.append('<tr  align="center"><td colspan=4>'+'字段信息'+'</td></tr>');
			for(var i=0;i<objsid.length;i++){
				var d = objsid[i];
				 var sdate = new Date(d.buildDate); 
		         var datestr =bu0(sdate.getMonth()+1)+'/'+bu0(sdate.getDate())+'/'+sdate.getFullYear();
		         table.append('<tr><td>字段英文名</td><td>'+d.dsName+'</td><td>'+'字段中文名'+'</td><td>'+d.beizhu+'</td></tr>');
					table.append('<tr><td>创建日期</td><td>'+datestr+'</td><td>'+'记录变更备注'+'</td><td>'+d.comments+'</td></tr>');
					table.append('<tr><td >数据类型</td><td >'+d.dataType+'</td><td>'+'是否为主键'+'</td><td>'+d.ispk+'</td></tr>');
					
		//	console.log(d);
			}
		});
	}
}

var zTree;
var zNodes;

$(function(){  
    $.getJSON("ztreeinit",{},function(data,status){ //请求成功    
    	 //alert("数据：" + data + "\n状态：" + status); 
    	 //console.log(data);
         zNodes = data;//把后台封装好的简单Json格式赋给zNodes 
     //   zTree = $.fn.zTree.init($("#tree2"), setting, zNodes);
         zTree = $.fn.zTree.init($("#tree1"), setting, zNodes);
         zTree = $.fn.zTree.init($("#tree3"), setting, zNodes);
    });  
}); 

function closed(){
	$("#mljg").hide();
	$("#ml").show();
	
}